<?php
	//Programming using PHP
	//Code Example : Seminarium 2
	//Haris Kljajic, Toni Kastrati, Frida Källberg, Per Möllberg, Zlatan Majdanac 
	//October 2012

	/**
	 * This class validates inputs for a registration form.
	 * Inputs that gets validates are following: Email, Personnumber, Password, Phonenumber.
	 */
	class Validation
	{
		private $m_password;
		
		/**
		 * Function which validates an email recived from a user/client.
		 * The function checks if the input is correct typed thru our regexp algorithm.
		 * 
		 * @return: True or false depending on if the email is inputed correctly.
		 */
		public function ValidateEmail($email)
		{
			$pattern = '^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$^';
			if(preg_match($pattern, $email))
			{
				return true;
			}
		}

		/**
		 * Function which validates an personnumber recived from a user/client.
		 * The function checks if the input is correct typed thru our luhn-algorithm.
		 * 
		 * In this function we split each number into an array with one index for each number.
		 * After that we loop thru the array and multiple every uneven number with two.
		 * At last we concat every index and get a result which must be able to modulus by 10 and equal to 0 to pass thru.
		 * 
		 * @return: True or false depending on if the personnumber is inputed correctly.
		 */
		public function ValidateID($ID)
		{	
			$digit = array();
			$result = array();	
			$sum = 0;
			$n = 0;
			$pattern = '^\b(19\d{2}|20\d{2}|\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])[-]?\d{4}\b$^';
			$arrayLength = strlen($ID);
			
			if(preg_match($pattern, $ID))
			{
				for($i = 0; $i < $arrayLength; $i++)
				{
					$digit[$i] = substr($ID, $i, 1);	
					
					if(is_numeric($digit[$i]))
					{
						$result[$n] = $digit[$i];
						$n++;
					} 
				}
				if(count($result) == 12)
				{
					$result = array_splice($result, 2, 12);
				}
				
				for ($i = 0; $i < 10; $i = $i + 1) 
				{
			        if ($i % 2 == 0) 
			        {
			        	 $result[$i] *= 2;
					}
			        if ($result[$i] > 9) 
			        {
			      		$result[$i] = $result[$i] - 9;
					}
			        $sum += $result[$i];
					
		    	}
	    		if($sum % 10 == 0)
				{
					return true;
				}
			}
		}
		
		public function ValidateDate($date)
		{		   
			$pattern = '^\b(19\d{2}|20\d{2}|\d{2})[-]?(0[1-9]|1[0-2])[-]?(0[1-9]|[12]\d|3[01])\b$^';
			if(preg_match($pattern, $date))
			{
				return true;
			}
		}
		
		public function ValidateString($string)
		{		   
			$pattern = '@<script[^>]*?>.*?</script>@si';
			if(!preg_match($pattern, $string) && !is_null($string))
			{
				return true;
			}
		}
		
		public function ValidateUsername($username)
		{		   
			$pattern = '^[a-z0-9_-]{3,40}$^';
			if(preg_match($pattern, $username))
			{
				return true;
			}
		}
		/**
		 * 
		 * 
		 * @return: True or false depending on if the password is inputed correctly.
		 */
		public function ValidatePassword($password)
		{
			$pattern = '^(?=.*\d).{4,8}$^';
			if(preg_match($pattern, $password))
			{
				$this->m_password = $password;
				return true;
			}
		}
		public function ValidatePasswordRepeat($passwordRepeat)
		{
			if ($passwordRepeat == $this->m_password) {
				return true;
			}
			
		}
		/**
		 * 
		 * 
		 * @return: True or false depending on if the phonenumber is inputed correctly.
		 */
		public function ValidateNumber($number)
		{
			$pattern = '^(([+]\d{2}[1-9]\d{0,2})|([0]\d{1,3}[-]))((\d{2}(\d{2}){2})|(\d{3}(\d{3})*(\d{2})+))$^';
			if(preg_match($pattern, $number))
			{
				return true;
			}
		}
		/**
		 * This function is an test to make it easier for us programmers to see if an
		 * function is working propertly as we want it to.
		 * 
		 * @return: True or false depending on if an function is wrongly implemented.
		 */
		public function Test()
		{
			if($this->ValidateEmail("eee@e-.com") || $this->ValidateEmail("haha@mail.se") == false)
			{
				echo "Email function doesn't work properly";
				return false;
			}
			if($this->ValidateID("19900827-2230") == false ||
			   $this->ValidateID("900827-2230") == false ||
			   $this->ValidateID("1990082--72230") == true ||
			   $this->ValidateID("9008272230") == false)
			{
				echo "ID function doesn't work properly";
				return false;
			}
			
			if($this->ValidateDate("2012-11-22") == false ||
				$this->ValidateDate("19920215") == false ||
				$this->ValidateDate("89090104") == true ||
				$this->ValidateDate("02-02-15") == false)
			{
				echo "Date function doesn't work properly";
				return false;   	
			}
			
			if($this->ValidateString("hej<b>ehj</b> ") == false ||
				$this->ValidateString("<script>alert('hej');</script>") == true)
			{
				echo "String function doesn't work properly";
				return false;   	
			}
			
			if($this->ValidateUsername("hej<b>ehj</b> ") == true ||
				$this->ValidateUsername("<script>alert('hej');</script>") == true ||
				$this->ValidateUsername("nigletboy_23847") == false)
			{
				echo "Username function doesn't work properly";
				return false;   	
			}
			
			if($this->ValidatePassword("asdsad") == true ||
				$this->ValidatePassword("<script>alert('hej');</script>") == true ||
				$this->ValidatePassword("asd") == true ||
				$this->ValidatePassword("asdsderfg") == true ||
				$this->ValidatePassword("frida@1") == false ) 
			{
				echo "Password function doesn't work properly";
				return false;   	
			}
			
			
			if($this->ValidatePassword("hej!1") == true &&
				$this->ValidatePasswordRepeat("hej!1") == false)	 
			{
				echo "PasswordRepeat function doesn't work properly";
				return false;   	
			}
			
			if($this->ValidateNumber("+46812345678") == false ||
			   $this->ValidateNumber("08-12345678") == false ||
			   $this->ValidateNumber("0123-45678") == false ||
			   $this->ValidateNumber("+4644244gg44") == true)
			   
			{
				echo "Number function doesn't work properly";
				return false;   	
			}
			
			echo "Everything works perfectly!";
			return true;
		}
	}
